
use "${data}\phina_flat.dta", clear
foreach x of varlist sup_plano_interno sup_uso_comun ejidatarios posesionarios  {
gen ln_`x'_phina = ln(1+`x')
}
la var ln_sup_plano_interno "Ln(ejido area, ha)"
la var ln_sup_uso_comun "Ln(common property area, ha)"
la var ln_ejidatarios "Ln(ejidatarios)"
la var ln_posesionarios "Ln(non-ejidatarios)"
rename estado estado_phina 
rename cve_edo cve_edo_phina

bysort clave: keep if _n == 1
keep clave ln_sup_plano_interno_phina ln_sup_uso_comun_phina ln_ejidatarios_phina ln_posesionarios_phina estado_phina cve_edo_phina
save "${data}\small_phina.dta", replace

use "${data}\universe.dta", clear

gen ben = treat20112012 ==1 | treat20132014 ==1

foreach x of varlist sup_plano_interno sup_uso_comun ejidatarios posesionarios area ///
dist_any_road_mt_EJawm dist_maj_city_km_EJawm dist_5000_city_km_EJawm {
gen ln_`x' = ln(1+`x')
}
la var ln_sup_plano_interno "Ln(ejido area, ha)"
la var ln_sup_uso_comun "Ln(common property area, ha)"
la var ln_ejidatarios "Ln(ejidatarios)"
la var ln_posesionarios "Ln(non-ejidatarios)"
la var ln_area "Ln(area submitted, ha)"
la var ln_dist_any_road_mt_EJawm  "Ln(km to any road)"
la var ln_dist_maj_city_km_EJawm  "Ln(km to major city)"
la var ln_dist_5000_city_km_EJawm "Ln(km to city $>$ 5,000)"
la var mean_canopy_per_EJawm  "Mean canopy cover"
la var per_maj_ind_EJawm  "\% indigenous in municipality"
la var defor_risk_mean_EJawm "Deforestation risk (INE)"
la var slope_deg_mean_EJawm "Average slope (deg)"
la var elev_met_mean_EJawm "Average elevation (m)"
drop _merge

estpost sum ln_sup_plano_interno ln_sup_uso_comun ln_ejidatarios ln_posesionarios ln_area ///
slope_deg_mean_EJawm elev_met_mean_EJawm ln_dist_any_road_mt_EJawm  ln_dist_maj_city_km_EJawm ///
ln_dist_5000_city_km_EJawm mean_canopy_per_EJawm per_maj_ind_EJawm defor_risk_mean_EJawm if insample == 1
estimates store A

estpost sum ln_sup_plano_interno ln_sup_uso_comun ln_ejidatarios ln_posesionarios ln_area ///
slope_deg_mean_EJawm elev_met_mean_EJawm ln_dist_any_road_mt_EJawm  ln_dist_maj_city_km_EJawm ///
ln_dist_5000_city_km_EJawm mean_canopy_per_EJawm per_maj_ind_EJawm defor_risk_mean_EJawm if insample == .
estimates store B

replace insample = 0 if insample == .

preserve
foreach var in ln_sup_plano_interno ln_sup_uso_comun ln_ejidatarios ln_posesionarios ln_area  ///
slope_deg_mean_EJawm elev_met_mean_EJawm ln_dist_any_road_mt_EJawm ln_dist_maj_city_km_EJawm ///
ln_dist_5000_city_km_EJawm mean_canopy_per_EJawm per_maj_ind_EJawm defor_risk_mean_EJawm  {
ttest `var', by (insample)
replace `var' = (r(mu_2) - r(mu_1))/(sqrt(r(sd_2)^2 + r(sd_1)^2))
}

estpost sum ln_sup_plano_interno ln_sup_uso_comun ln_ejidatarios ln_posesionarios ln_area ///
slope_deg_mean_EJawm elev_met_mean_EJawm ln_dist_any_road_mt_EJawm ln_dist_maj_city_km_EJawm ///
ln_dist_5000_city_km_EJawm mean_canopy_per_EJawm per_maj_ind_EJawm defor_risk_mean_EJawm 
estimates store C
restore

bysort clave: keep if _n == 1
merge 1:1 clave using "${data}\small_phina.dta"

gen test = 1 if insample == 1
replace test = 0 if _merge == 2

foreach var in ln_sup_plano_interno ln_sup_uso_comun ln_ejidatarios ln_posesionarios {
replace `var' = `var'_phina if _merge == 2
}
estpost sum ln_sup_plano_interno ln_sup_uso_comun ln_ejidatarios ln_posesionarios if _merge == 2
estimates store D


preserve
foreach var in ln_sup_plano_interno ln_sup_uso_comun ln_ejidatarios ln_posesionarios {
ttest `var', by(test)
replace `var' = (r(mu_2) - r(mu_1))/(sqrt(r(sd_2)^2 + r(sd_1)^2))
}
estpost sum ln_sup_plano_interno ln_sup_uso_comun ln_ejidatarios ln_posesionarios 
estimates store E

restore 


estout A B C D E using "${outputs}\sample_v_universe.tex", replace cells("mean(fmt(2))") label style (tex) ///
prehead (\begin{tabular}{lccccc} \hline  \hline  & Sampled &  Not sampled & & Not in universe \\  & (1) & (2) & (3) & (4) & (5) \\ ///
& & & (1) v (2) & (1) v (4) \\ \cline{2-5} ) ///
mlabels( none)  collabels(none) stats(N, labels(Obs)  fmt(%9.0g)) end(\\ \addlinespace) ///
postfoot(\hline \hline \end{tabular}  \begin{tablenotes}[para,flushleft] \footnotesize{Columns (1) and (2) show ///
mean values of variables for communities that were sampled ///
versus those that were not sampled. Column (3) presents the normalized difference between these two.  ///
The fourth column shows means for variables available in Mexico's PHINA database, which gives ///
information on all agrarian communities existing as of November 2016. The last column presents the normalized differences in means ///
between the sampled communities and nationwide PHINA database. } \end{tablenotes} )

	